/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import Beans.Account;
import Encrypt.MD5;
import Encrypt.StringMng;
import Service.Impl.AccountManagerImpl;
import Service.Impl.ValidateImpl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Administrator
 */
public class AccountManagerFrm extends javax.swing.JFrame {

    Vector list;
    Vector column;
    AccountManagerImpl accMng;
    Account accDefault;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    int flag;
    
    /**
     * Creates new form AccountManager
     */
    public AccountManagerFrm() {
        initComponents();
        accMng = new AccountManagerImpl();
        list = new Vector();
        column = new Vector();
        accDefault = new Account();
        column.addElement("AccountNo");
        column.addElement("Password");
        column.addElement("Name");
        column.addElement("BirthDay");
        column.addElement("Organization");
        column.addElement("Address");
        column.addElement("Email");
        column.addElement("Phone");
        column.addElement("Salary");
        column.addElement("RegisterDate");
        column.addElement("Status");
        reloadData();
    }

    private void reloadData() {
        list = accMng.getAll();
        txtSearch.setText("");
        lbMessage.setText("");
        TableModel model = new DefaultTableModel(list, column);
        tblAccountList.setModel(model);
        tblAccountList.removeColumn(tblAccountList.getColumn("Password"));
        resetTextForm();
        txtAccountNo.setEditable(false);
        flag=0;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        txtSearch = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        btnDetails = new javax.swing.JButton();
        lblUsername10 = new javax.swing.JLabel();
        cbStatus = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblAccountList = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        lblUsername = new javax.swing.JLabel();
        txtAccountNo = new javax.swing.JTextField();
        lblUsername1 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        lblUsername2 = new javax.swing.JLabel();
        lblUsername3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtOrganization = new javax.swing.JTextArea();
        lblUsername4 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtAddress = new javax.swing.JTextArea();
        lblUsername5 = new javax.swing.JLabel();
        lblUsername6 = new javax.swing.JLabel();
        lblUsername7 = new javax.swing.JLabel();
        txtSalary = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        txtPhone = new javax.swing.JTextField();
        lblUsername8 = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        btnSave = new javax.swing.JButton();
        lbMessage = new javax.swing.JLabel();
        btnNew = new javax.swing.JButton();
        dateBirthDay = new com.toedter.calendar.JDateChooser();
        lblUsername9 = new javax.swing.JLabel();
        chkStatus = new javax.swing.JCheckBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mnOpen = new javax.swing.JMenuItem();
        mnNew = new javax.swing.JMenuItem();
        mnSave = new javax.swing.JMenuItem();
        mnRefresh = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mnExit = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Account Manager");
        setPreferredSize(new java.awt.Dimension(940, 594));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setPreferredSize(new java.awt.Dimension(671, 59));

        txtSearch.setToolTipText("Enter key search");
        txtSearch.setPreferredSize(new java.awt.Dimension(6, 23));

        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Search1.jpg"))); // NOI18N
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        btnDetails.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/address_16.png"))); // NOI18N
        btnDetails.setText("View Details");
        btnDetails.setEnabled(false);
        btnDetails.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetailsActionPerformed(evt);
            }
        });

        lblUsername10.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsername10.setText("Status");

        cbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "Adopted", "Block" }));
        cbStatus.setPreferredSize(new java.awt.Dimension(66, 25));
        cbStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbStatusActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnDetails, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 289, Short.MAX_VALUE)
                .addComponent(lblUsername10, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSearch)
                .addGap(248, 248, 248))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSearch)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnDetails)
                        .addComponent(lblUsername10)
                        .addComponent(cbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_START);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setLayout(new java.awt.BorderLayout());

        tblAccountList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "AccountNo", "Name", "BirthDay", "Organization", "Address", "Email", "Phone", "Salary", "RegisterDate", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblAccountList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblAccountList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAccountListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblAccountList);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Information"));

        lblUsername.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsername.setText("AccountNo");

        txtAccountNo.setEditable(false);

        lblUsername1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsername1.setText("Name");

        lblUsername2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsername2.setText("BirthDay");

        lblUsername3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsername3.setText("Organization");

        jScrollPane2.setPreferredSize(new java.awt.Dimension(166, 56));

        txtOrganization.setColumns(20);
        txtOrganization.setRows(5);
        jScrollPane2.setViewportView(txtOrganization);

        lblUsername4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsername4.setText("Address");

        txtAddress.setColumns(20);
        txtAddress.setRows(5);
        jScrollPane3.setViewportView(txtAddress);

        lblUsername5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsername5.setText("Email");

        lblUsername6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsername6.setText("Phone");

        lblUsername7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsername7.setText("Salary");

        lblUsername8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsername8.setText("Password");

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/save-16x16.png"))); // NOI18N
        btnSave.setText("save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        lbMessage.setForeground(new java.awt.Color(255, 0, 51));

        btnNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/add.png"))); // NOI18N
        btnNew.setText("New");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });

        dateBirthDay.setDate(new java.util.Date(1343873113000L));
        dateBirthDay.setDateFormatString("dd-MM-yyyy");

        lblUsername9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsername9.setText("Status");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtName)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
                    .addComponent(jScrollPane3)
                    .addComponent(dateBirthDay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(lblUsername5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(13, 13, 13)
                        .addComponent(txtEmail))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblUsername6, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblUsername7, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtSalary, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                            .addComponent(txtPhone)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(btnNew)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSave)
                        .addGap(21, 21, 21))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblUsername1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblUsername2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblUsername3, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblUsername4, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(lblUsername8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(lblUsername, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtAccountNo)
                                    .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)))
                            .addComponent(lbMessage)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(lblUsername9, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(13, 13, 13)
                                .addComponent(chkStatus)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsername)
                    .addComponent(txtAccountNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblUsername8))
                .addGap(6, 6, 6)
                .addComponent(lblUsername1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblUsername2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dateBirthDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(lblUsername3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblUsername4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsername5)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsername6)
                    .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsername7)
                    .addComponent(txtSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblUsername9, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(chkStatus, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNew)
                    .addComponent(btnSave))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbMessage)
                .addContainerGap())
        );

        lblUsername4.getAccessibleContext().setAccessibleName("");

        jPanel3.add(jPanel4, java.awt.BorderLayout.LINE_END);

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jMenu1.setText("File");

        mnOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        mnOpen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Open.png"))); // NOI18N
        mnOpen.setText("Open");
        mnOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnOpenActionPerformed(evt);
            }
        });
        jMenu1.add(mnOpen);

        mnNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        mnNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/button-new-icon.png"))); // NOI18N
        mnNew.setText("New");
        mnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnNewActionPerformed(evt);
            }
        });
        jMenu1.add(mnNew);

        mnSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        mnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/save-icon.jpg"))); // NOI18N
        mnSave.setText("Save");
        mnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnSaveActionPerformed(evt);
            }
        });
        jMenu1.add(mnSave);

        mnRefresh.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        mnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Refresh-icon.jpg"))); // NOI18N
        mnRefresh.setText("Refresh");
        mnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnRefreshActionPerformed(evt);
            }
        });
        jMenu1.add(mnRefresh);
        jMenu1.add(jSeparator1);

        mnExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        mnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/exit.jpg"))); // NOI18N
        mnExit.setText("Exit");
        mnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnExitActionPerformed(evt);
            }
        });
        jMenu1.add(mnExit);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("Help");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/help.png"))); // NOI18N
        jMenuItem1.setText("How to use");
        jMenu3.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, java.awt.event.InputEvent.SHIFT_MASK));
        jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/information.png"))); // NOI18N
        jMenuItem2.setText("About");
        jMenu3.add(jMenuItem2);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnOpenActionPerformed
        // TODO add your handling code here:
        AccountDetailsFrm form = new AccountDetailsFrm();
        form.setVisible(true);
        form.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        form.setLocationRelativeTo(null);

                
    }//GEN-LAST:event_mnOpenActionPerformed

    private void mnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnExitActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_mnExitActionPerformed

    private void btnDetailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetailsActionPerformed
        // TODO add your handling code here:
       if(!txtAccountNo.getText().equals(""))
       {
            AccountDetailsFrm form = new AccountDetailsFrm();
            form.acc = accMng.selectRow(txtAccountNo.getText().trim());
            form.setVisible(true);
            form.setLocationRelativeTo(null);
            form.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
       }
        
    }//GEN-LAST:event_btnDetailsActionPerformed

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        // TODO add your handling code here:
        resetTextForm();
        String txt=accMng.generateAccountNo();
        txtAccountNo.setText(txt);
        flag=1;
    }//GEN-LAST:event_btnNewActionPerformed
    /*
     * reset text for form information
     */
    private void resetTextForm() {
        txtAccountNo.setText("");
        txtPassword.setText("");
        txtName.setText("");
        txtOrganization.setText("");
        txtAddress.setText("");
        txtEmail.setText("");
        txtPhone.setText("");
        txtSalary.setText("");
    }
   // method process button save and menu save
    private void save() {
        // insert or update
        if (isValidate()) {
            if (flag==1) {
                // insert new Account        
               setAccountDefault();
               accDefault.setRegisterDate(getCurentDate());
               accMng.insertRow(accDefault);                
            } else {
               // update
               setAccountDefault();
               accMng.updateRow(accDefault);
            }
            reloadData();
        }
        
    }
    
    private void tblAccountListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAccountListMouseClicked
        // TODO add your handling code here:
        int index = tblAccountList.getSelectedRow();
        if (index > -1) {
                AccountManagerImpl accMng= new AccountManagerImpl();
                String accNo = (String) tblAccountList.getValueAt(index,0);
                accDefault = accMng.selectRow(accNo);
                txtAccountNo.setText(accDefault.getAccountNo());
                txtPassword.setText("");
                txtName.setText(accDefault.getName());
                dateBirthDay.setDate(accDefault.getBirthday());
                txtOrganization.setText(accDefault.getOrganization());
                txtAddress.setText(accDefault.getAddress());
                txtEmail.setText(accDefault.getEmail());
                txtPhone.setText(accDefault.getPhone());
                txtSalary.setText(accDefault.getSalary()+"");
                chkStatus.setSelected(accDefault.isStatus());
                btnDetails.setEnabled(true);   
            
        }
    }//GEN-LAST:event_tblAccountListMouseClicked
   // set value from form Information to object accDefault 
    private void setAccountDefault() {
            accDefault.setAccountNo(txtAccountNo.getText());
            accDefault.setPassword(MD5.encrypt(new String(txtPassword.getPassword())));
            accDefault.setAddress(txtAddress.getText());
            accDefault.setName(StringMng.getName(txtName.getText()));
            
            accDefault.setBirthday(new java.sql.Date(dateBirthDay.getDate().getTime()));
            accDefault.setEmail(txtEmail.getText());
            accDefault.setOrganization(txtOrganization.getText());
            accDefault.setPhone(txtPhone.getText());
            accDefault.setSalary(Integer.parseInt(txtSalary.getText()));
            accDefault.setStatus(chkStatus.isSelected());
    }

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        save();
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        list= accMng.search(txtSearch.getText().trim());  
        TableModel model = new DefaultTableModel(list, column);
        tblAccountList.setModel(model);
        tblAccountList.removeColumn(tblAccountList.getColumn("Password")); 
    }//GEN-LAST:event_btnSearchActionPerformed

    private void mnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnNewActionPerformed
        // TODO add your handling code here:
        txtAccountNo.setEditable(true);
        resetTextForm();
    }//GEN-LAST:event_mnNewActionPerformed

    private void mnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnSaveActionPerformed
        // TODO add your handling code here
        save();
    }//GEN-LAST:event_mnSaveActionPerformed

    private void mnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnRefreshActionPerformed
        // TODO add your handling code here:
        reloadData();
    }//GEN-LAST:event_mnRefreshActionPerformed

    private void cbStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbStatusActionPerformed
        // select change conbobox
        boolean val;
        if(cbStatus.getSelectedIndex()==1)
            val= true;
        else if(cbStatus.getSelectedIndex()==2)
            val= false;
        else{
            TableModel model = new DefaultTableModel(list, column);
            tblAccountList.setModel(model);
            tblAccountList.removeColumn(tblAccountList.getColumn("Password"));
            return;
        }             
        Vector smallList = new Vector();
        for(Object ob : list)
        {
           Vector row = (Vector)ob;
           if((boolean)row.get(10)== val)
           {
              smallList.addElement(row);
           }
        }
        TableModel model = new DefaultTableModel(smallList, column);
        tblAccountList.setModel(model);
        tblAccountList.removeColumn(tblAccountList.getColumn("Password"));
    }//GEN-LAST:event_cbStatusActionPerformed

    /*
     * validate form information
     * return true or false
     */
    private boolean isValidate() {
       ValidateImpl valid= new ValidateImpl();
        
       if(txtAccountNo.getText().equals("") || txtPassword.getPassword().length==0
          || txtName.getText().equals("")
          || txtAddress.getText().equals("") 
          || txtEmail.getText().equals("") || txtOrganization.getText().equals("")
          || txtPhone.getText().equals("") || txtSalary.getText().equals("")     
       ){
          lbMessage.setText("Can not register because has textbox is null");
          return false;
       }
       
       // check BirthDay (year old >= 18)
        java.sql.Date birthDay = new java.sql.Date(dateBirthDay.getDate().getTime());
        Calendar calendar= GregorianCalendar.getInstance();
        calendar.setTime(birthDay);
        int  birthYear = calendar.get(Calendar.YEAR);
        calendar.setTime(getCurentDate());
        int curentYear= calendar.get(Calendar.YEAR);
        int old= curentYear - birthYear ;
        if(old<18){
            lbMessage.setText("your old is'nt enough 18!");
            return false;    
        }
       // validate email
        if(!valid.isEmail(txtEmail.getText()))
        {
           lbMessage.setText("Email not true format");
           return false;
        }
        
       // validate salary
        if(!valid.isNumber(txtSalary.getText()))
        {
           lbMessage.setText("Salary is not integer number");
           return false;
        }
       //validate Phone Number
        if(!valid.isPhone(txtPhone.getText().trim()))
        {
            lbMessage.setText("Phone number must is number and length from 10 to 12");
            return false;
        }
       return true;   
    }

    /*
     * return curentDate
     */
    public java.sql.Date getCurentDate() {
        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        return sqlDate;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDetails;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cbStatus;
    private javax.swing.JCheckBox chkStatus;
    private com.toedter.calendar.JDateChooser dateBirthDay;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JLabel lbMessage;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JLabel lblUsername1;
    private javax.swing.JLabel lblUsername10;
    private javax.swing.JLabel lblUsername2;
    private javax.swing.JLabel lblUsername3;
    private javax.swing.JLabel lblUsername4;
    private javax.swing.JLabel lblUsername5;
    private javax.swing.JLabel lblUsername6;
    private javax.swing.JLabel lblUsername7;
    private javax.swing.JLabel lblUsername8;
    private javax.swing.JLabel lblUsername9;
    private javax.swing.JMenuItem mnExit;
    private javax.swing.JMenuItem mnNew;
    private javax.swing.JMenuItem mnOpen;
    private javax.swing.JMenuItem mnRefresh;
    private javax.swing.JMenuItem mnSave;
    private javax.swing.JTable tblAccountList;
    private javax.swing.JTextField txtAccountNo;
    private javax.swing.JTextArea txtAddress;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextArea txtOrganization;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtSalary;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
}
